package com.newear.datacenter;


import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.util.Log;

import com.newear.MyApplication;
import com.newear.base.http.OKHttpClientManager;
import com.newear.constant.URLConstant;
import com.newear.util.DataConverter;
import com.newear.util.HardWare;
import com.newear.util.MyConstant;
import com.newear.util.MyLogger;
import com.newear.util.Validator;

import java.io.File;

/**
 * Created by josan_tang on 2015/12/6.
 * 远程服务端类
 * 封装请求参数，比如（地理位置，版本号等基本信息）
 * 调用网络基础层，得到Json格式数据返回
 * 所有方法为static方法，不能创建对象
 */
public class RemoteServer {
    private static final String TAG = "RemoteServer";

    public RemoteServer() {
        throw new UnsupportedOperationException("RemoteServer所有方法为static方法，不能创建对象");
    }

    /**
     * 得到报告
     *
     * @return json数据
     */
    public static String getReport(Context appContext) {
        ArrayMap<String, String> params = HardWare.getBaseParams(appContext);
        String url = URLConstant.BASE_URL + "/examination";
        String requestJson = HardWare.getRequestReportJson();
        MyLogger.e(TAG, "getReport url:" + url + "\nrequestJson:" + requestJson);
        if (!Validator.isEffective(requestJson)) {
            return "";
        }
        String result = OKHttpClientManager.postJson(appContext, url, requestJson);
        Log.e(TAG, "getReport result:" + result);
        return result;
    }

    /**
     * 得到报告
     *
     * @return json数据
     */
    public static String getReportById(Context appContext, int id) {
        ArrayMap<String, String> params = HardWare.getBaseParams(appContext);
        String url = URLConstant.BASE_URL + "/report/" +id;
        MyLogger.e(TAG, "getReportById url:" + url);
        String result = OKHttpClientManager.getString(appContext, url);
        Log.e(TAG, "getReportById result:" + result);
        return result;
    }

    /**
     * 得到历史报告
     * @return
     */
    public static String getHistoryReport(Context appContext, String date, int page) {
        ArrayMap<String, String> params = HardWare.getBaseParams(appContext);
        String url = URLConstant.BASE_URL + "/report?curr=" + page + "&start_date=" + date;
        MyLogger.e(TAG, "getHistoryReport url:" + url + "\nparams:" + DataConverter.createParamByMap(params));
        String result = OKHttpClientManager.getString(appContext, url);
        Log.e(TAG, "getHistoryReport result:" + result);
        return result;
    }

    /**
     * 用户登录
     *
     * @param loginName
     * @param loginPwd
     * @return json数据
     */
    public static String userLogin(Context appContext, String loginName, String loginPwd) {
        try {
            ArrayMap<String, String> params = HardWare.getBaseParams(appContext);
            if (Validator.isEffective(loginName)) {
                params.put("user", loginName);
            }
            if (Validator.isEffective(loginPwd)) {
                params.put("passwd", loginPwd);
            }

            String url = URLConstant.BASE_URL + "/xsd/login";
            MyLogger.e(TAG, "userLogin url:" + url + "\nparams:" + DataConverter.createParamByMap(params));

            String result = OKHttpClientManager.postJson(url, params);
            MyLogger.e(TAG, "userLogin result:" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getWeather(Context appContext, double lat, double lon) {
        try {
            String url = URLConstant.BASE_URL + "/xsd/weather?" + "location=" + lon + "," + lat + "&base=true";
            MyLogger.e(TAG, "getWeather url:" + url);

            String result = OKHttpClientManager.getString(appContext, url);
            MyLogger.e(TAG, "getWeather result:" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 上传脸部照片
     * @param context
     * @return
     */
    public static String uploadFacePhoto(Context context) {
        ArrayMap<String, String> params = HardWare.getBaseParams(context);

        String url = URLConstant.BASE_URL + "/xsd/face";
        MyLogger.e(TAG, "uploadFacePhoto url:" + url + "\nparams:" + DataConverter.createParamByMap(params));
        File dir = new File(MyConstant.BASE_PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(MyConstant.BASE_PATH + "/face.png");
        HardWare.getFilePathByBitmap(MyApplication.sCustomerInfo.faceBitmap, file.getAbsolutePath());
        String result = OKHttpClientManager.postUpload(url, file, "photo", params);
        MyLogger.e(TAG, "uploadFacePhoto result:" + result);

        return result;
    }
    /**
     * 上传脸部照片
     * @param context
     * @return
     */
    public static String uploadTonguePhoto(Context context) {
        ArrayMap<String, String> params = HardWare.getBaseParams(context);
        String url = URLConstant.BASE_URL + "/xsd/tongue";
        MyLogger.i(TAG, "uploadTonguePhoto url:" + url + "\nparams:" + DataConverter.createParamByMap(params));
        File dir = new File(MyConstant.BASE_PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(MyConstant.BASE_PATH + "/tongue.png");
        HardWare.getFilePathByBitmap(MyApplication.sCustomerInfo.tongueBitmap, file.getAbsolutePath());
        String result = OKHttpClientManager.postUpload(url, file, "photo", params);
        MyLogger.i(TAG, "uploadTonguePhoto result:" + result);
        return result;
    }

    /**
     * 检验VIP
     * @param appContext
     * @param id
     * @return
     */
    public static String checkVip(Context appContext, String id) {
        String url = URLConstant.BASE_URL + "/vip/check/" + id;
        MyLogger.e(TAG, "checkVip url:" + url);
        String result = OKHttpClientManager.getString(appContext, url);
        Log.e(TAG, "checkVip result:" + result);
        return result;
    }

}
